package com.iron.visual.minio.controller;

import com.alibaba.fastjson.JSONObject;
import com.jvm123.minio.service.MinioFileService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author yt
 * @date 2020/6/19
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class MinioController {

    private final MinioFileService fileService;

    @GetMapping("/test")
    public void test(HttpServletResponse response) throws IOException {
        String bucketName = "test";

        // 创建bucket
//        boolean created = fileService.createBucket(bucketName);

        // 存储文件
        String saved = fileService.save(bucketName, new File(), "a.txt");

        // 删除文件
//        boolean deleted = fileService.delete(bucketName, "a.txt");

        // 获取文件(会产生本地缓存)
//        File file = fileService.getFile(bucketName, "TIM截图20200417175430.jpg");

        // 获取输入流
//        InputStream inputStream = fileService.getStream(bucketName, "a.txt");

        // 下载
        response.addHeader("Content-Disposition","attachment;filename=TIM截图20200417175430.jpg");
        ServletOutputStream os = response.getOutputStream();
        fileService.writeTo(bucketName, "TIM截图20200417175430.jpg", os);
    }
}
